#
# This file is run prior to the unit tests for preparing the testing
# environment. Currently only required / necessary for running the
# tests on the Android device emulator.
#

# Restore the required settings of the CMake configuration step
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
SET(CMAKE_COMMAND "@CMAKE_COMMAND@")
SET(CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@")
SET(CMAKE_CURRENT_BINARY_DIR "@CMAKE_BINARY_DIR@")
SET(CMAKE_FILES_DIRECTORY "@CMAKE_FILES_DIRECTORY@")
SET(CMAKE_MODULE_PATH "@CMAKE_ROOT@/Modules")
SET(CMAKE_SHARED_LIBRARY_PREFIX "@CMAKE_SHARED_LIBRARY_PREFIX@")
SET(CMAKE_SHARED_LIBRARY_SUFFIX "@CMAKE_SHARED_LIBRARY_SUFFIX@")
SET(CMAKE_HOST_SYSTEM "@CMAKE_HOST_SYSTEM@")
SET(DCMTK_CMAKE_INCLUDE "@CMAKE_SOURCE_DIR@/@DCMTK_CMAKE_INCLUDE@")
SET(DCMTK_TEST_EXECUTABLES "@DCMTK_TEST_EXECUTABLES@")
SET(DCMTK_ALL_LIBRARIES "@DCMTK_ALL_LIBRARIES@")
SET(DCMTK_LIBRARY_DEPENDENCIES "@DCMTK_LIBRARY_DEPENDENCIES@")
SET(DCMTK_PACKAGE_VERSION "@DCMTK_PACKAGE_VERSION@")
SET(DCMTK_ABI_VERSION "@DCMTK_ABI_VERSION@")
SET(BUILD_SHARED_LIBS "@BUILD_SHARED_LIBS@")
SET(DCMTK_DICOM_DICTIONARIES "@DCMTK_DICOM_DICTIONARIES@")
SET(ANDROID "@ANDROID@")
SET(ANDROID_ADB_PROGRAM "@ANDROID_ADB_PROGRAM@")
SET(ANDROID_ANDROID_PROGRAM "@ANDROID_ANDROID_PROGRAM@")
SET(ANDROID_EMULATOR_PROGRAM "@ANDROID_EMULATOR_PROGRAM@")
SET(ANDROID_EMULATOR_AVD "@ANDROID_EMULATOR_AVD@")
SET(ANDROID_RUNTIME_LIBRARIES "@ANDROID_RUNTIME_LIBRARIES@")
SET(ANDROID_TEMPORARY_FILES_LOCATION "@ANDROID_TEMPORARY_FILES_LOCATION@")
# Emulate some required CMake commands while running inside CTest
INCLUDE(${DCMTK_CMAKE_INCLUDE}CMake/CTest/dcmtkCTestMacros.cmake)
# For DCMTK_UNSET_XXX
INCLUDE(${DCMTK_CMAKE_INCLUDE}CMake/dcmtkMacros.cmake)
# Load required functions for controlling the Android device emulator
INCLUDE(${DCMTK_CMAKE_INCLUDE}CMake/dcmtkUseAndroidSDK.cmake)

# Prepare the Android testing environment
# Prepare the emulator
DCMTK_ANDROID_START_EMULATOR(DCMTK_ANDROID_EMULATOR_INSTANCE)
DCMTK_ANDROID_WAIT_FOR_EMULATOR(DCMTK_ANDROID_EMULATOR_INSTANCE)

# Get the emulator's name for emitting a shutdown command when done
DCMTK_ANDROID_GET_OBJECT_PROPERTIES(DCMTK_ANDROID_EMULATOR_INSTANCE)
DCMTK_ANDROID_STOP_EMULATOR_COMMAND(STOP_EMULATOR "${EMULATOR_NAME}")
DCMTK_ANDROID_EMULATOR_SHUTDOWN_MESSAGE(STOP_MESSAGE)
# Note: no quotation marks around STOP_MESSAGE, since it contains the message
#       and the message type (e.g. STATUS)
MESSAGE_COMMAND(STOP_MESSAGE ${STOP_MESSAGE})
# CTEST_CUSTOM_POST_TESTS executes a sequence of 'non CMake' commands separated
# by ';' characters. Therefore the sequence a single command consists of must
# be translated appropriately first
STRING(REPLACE ";" " " STOP_EMULATOR "${STOP_EMULATOR}")
STRING(REPLACE ";" " " STOP_MESSAGE "${STOP_MESSAGE}")
# Register the cleanup hooks
SET(CTEST_CUSTOM_POST_TEST "${STOP_MESSAGE}" "${STOP_EMULATOR}")

# Set the current emulator instance handle via an environment variable
SET(ENV{DCMTK_ANDROID_EMULATOR_INSTANCE} "${DCMTK_ANDROID_EMULATOR_INSTANCE}")

# Collect the list of required libraries
IF(BUILD_SHARED_LIBS)
    FOREACH(LIBRARY ${DCMTK_ALL_LIBRARIES})
        LIST(APPEND DCMTK_CREATED_SHARED_LIBRARIES "${CMAKE_BINARY_DIR}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}${LIBRARY}${CMAKE_SHARED_LIBRARY_SUFFIX}.${DCMTK_PACKAGE_VERSION}")
    ENDFOREACH()
ENDIF(BUILD_SHARED_LIBS)

# Transmit the required executables, libraries and dictionaries to the emulator
DCMTK_ANDROID_PUSH(DCMTK_ANDROID_EMULATOR_INSTANCE
    ${ANDROID_RUNTIME_LIBRARIES}
    ${DCMTK_LIBRARY_DEPENDENCIES}
    ${DCMTK_CREATED_SHARED_LIBRARIES}
    ${DCMTK_TEST_EXECUTABLES}
    ${DCMTK_DICOM_DICTIONARIES}
    DESTINATION ${ANDROID_TEMPORARY_FILES_LOCATION}
)

# Set executable permissions
FOREACH(TEST_EXECUTABLE ${DCMTK_TEST_EXECUTABLES})
    GET_FILENAME_COMPONENT(NAME "${TEST_EXECUTABLE}" NAME)
    DCMTK_ANDROID_SHELL(DCMTK_ANDROID_EMULATOR_INSTANCE
        COMMAND chmod 755 "${ANDROID_TEMPORARY_FILES_LOCATION}/${NAME}"
        OUTPUT_QUIET
        ERROR_QUIET
    )
ENDFOREACH()

# Create necessary softlinks
IF(BUILD_SHARED_LIBS)
    FOREACH(LIBRARY ${DCMTK_ALL_LIBRARIES})
        DCMTK_ANDROID_SHELL(DCMTK_ANDROID_EMULATOR_INSTANCE
        COMMAND ln -s "./${CMAKE_SHARED_LIBRARY_PREFIX}${LIBRARY}${CMAKE_SHARED_LIBRARY_SUFFIX}.${DCMTK_PACKAGE_VERSION}" "${ANDROID_TEMPORARY_FILES_LOCATION}/${CMAKE_SHARED_LIBRARY_PREFIX}${LIBRARY}${CMAKE_SHARED_LIBRARY_SUFFIX}.${DCMTK_ABI_VERSION}"
        OUTPUT_QUIET
        ERROR_QUIET
        )
    ENDFOREACH()
ENDIF(BUILD_SHARED_LIBS)
